# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=docutils
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=0.18.1
pkgrel=5
pkgdesc="Set of tools for processing plaintext docs into formats such as HTML, XML, or LaTeX (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('custom')
url="https://docutils.sourceforge.io/"
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-python-build"
  "${MINGW_PACKAGE_PREFIX}-python-installer"
  "${MINGW_PACKAGE_PREFIX}-python-setuptools"
  "${MINGW_PACKAGE_PREFIX}-python-wheel"
)
source=(https://downloads.sourceforge.net/${_realname}/${_realname}-${pkgver}.tar.gz
        pyscript2exe.py)
sha256sums=('679987caf361a7539d76e584cbeddc311e3aee937877c87346f31debc63e9d06'
            'e57174517b08cf8f9fb4f43a381d342d23d2db3cad661107f35ca21c39b5734d')
noextract=(${_realname}-${pkgver}.tar.gz)

prepare() {
  tar -xzf ${srcdir}/${_realname}-${pkgver}.tar.gz -C ${srcdir} || true

  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"
}

build() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  PYTHONUTF8=1 PYTHONPATH="$PWD/build/python/" ${MINGW_PREFIX}/bin/python test/alltests.py
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    ${MINGW_PREFIX}/bin/python \
      "${srcdir}/pyscript2exe.py" "${_f}"
  done

  # setup license
  install -D -m644 COPYING.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/COPYING.txt"
  install -D -m644 licenses/python* "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/"
}
